#include "bsp_mc_drv8323.h"
#include "bsp_mc_adc.h"

volatile u16 TxData=0,RxData=0;

void SPI_GPIO_Init(void)
{

	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_AHB1PeriphClockCmd( MOTOR_SPI_GPIO_CLK, ENABLE );//SPI clock
	RCC_AHB1PeriphClockCmd( MOTOR_SPI_CS_CLK, ENABLE );  //SPI CS clock
	
	GPIO_PinAFConfig( MOTOR_SPI_GPIO_PORT, MOTOR_SPI_CLK_PINSource, GPIO_AF_SPI3 ); 
	GPIO_PinAFConfig( MOTOR_SPI_GPIO_PORT, MOTOR_SPI_MISO_PINSource, GPIO_AF_SPI3 ); 
	GPIO_PinAFConfig( MOTOR_SPI_GPIO_PORT, MOTOR_SPI_MOSI_PINSource, GPIO_AF_SPI3 ); 
	
	GPIO_InitStructure.GPIO_Pin = MOTOR_SPI_CLK_PIN | MOTOR_SPI_MISO_PIN | MOTOR_SPI_MOSI_PIN;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	
	GPIO_Init(MOTOR_SPI_GPIO_PORT,&GPIO_InitStructure);
	
	
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;  
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_Pin   = MOTOR_SPI_CS_PIN;	
	
	GPIO_Init(MOTOR_SPI_CS_GPIO_PORT, &GPIO_InitStructure);	
	
	RCC_AHB1PeriphClockCmd( DRV8323_CAL_GPIO_CLK, ENABLE );//drv8323 cal clock
	GPIO_InitStructure.GPIO_Pin   = DRV8323_CAL_PIN;
	GPIO_Init(DRV8323_CAL_GPIO_PORT,&GPIO_InitStructure);
	
	RCC_AHB1PeriphClockCmd( DRV8323_POWER_GPIO_CLK, ENABLE );//drv8323 24V power on
	GPIO_InitStructure.GPIO_Pin   = DRV8323_POWER_PIN;
	GPIO_Init(DRV8323_POWER_GPIO_PORT,&GPIO_InitStructure);
	
	RCC_AHB1PeriphClockCmd( DRV8323_ENABLE_GPIO_CLK, ENABLE );//drv8323 enable  clock
	GPIO_InitStructure.GPIO_Pin   = DRV8323_ENABLE_PIN;
	GPIO_Init(DRV8323_ENABLE_GPIO_PORT,&GPIO_InitStructure);
	

	RCC_AHB1PeriphClockCmd( Test_GPIO_CLK, ENABLE );// Test Pin clock
	GPIO_InitStructure.GPIO_Pin   = Test_PIN;
	GPIO_Init(Test_GPIO_PORT,&GPIO_InitStructure);
	

	RCC_AHB1PeriphClockCmd( DRV8323_FAULT_GPIO_CLK, ENABLE );// fault Pin clock
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN;  
	GPIO_InitStructure.GPIO_Pin   = DRV8323_FAULT_PIN;
	GPIO_Init(DRV8323_FAULT_GPIO_PORT,&GPIO_InitStructure);
	

}


void SPI_Mode_Init(void)
{
	
	SPI_InitTypeDef  SPI_InitStructure;
	
	RCC_APB1PeriphClockCmd( MOTOR_SPI_CLK, ENABLE );
	
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;//clock 42M/16=2.625MHZ
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial = 7;
	
	SPI_Init(MOTOR_SPI, &SPI_InitStructure);

	SPI_Cmd(MOTOR_SPI, ENABLE);
}

u16 Spi_Send_HalfWord(u16 sendata)
{

	GPIO_ResetBits(MOTOR_SPI_CS_GPIO_PORT, MOTOR_SPI_CS_PIN);
	while (SPI_I2S_GetFlagStatus(MOTOR_SPI, SPI_I2S_FLAG_TXE) == RESET);
	SPI_I2S_SendData(MOTOR_SPI, sendata);

	while (SPI_I2S_GetFlagStatus(MOTOR_SPI, SPI_I2S_FLAG_RXNE) == RESET);
	GPIO_SetBits(MOTOR_SPI_CS_GPIO_PORT, MOTOR_SPI_CS_PIN);
	
	return SPI_I2S_ReceiveData(MOTOR_SPI);


}

 void motor_power_on(void)
{
    int i;
    int j;

    for (i = 0; i < 10000; ++i) {
        for (j = 0; j < 100; ++j) {
            if (j > i / 100) {
				GPIO_ResetBits(DRV8323_POWER_GPIO_PORT,DRV8323_POWER_PIN);
            }
            else {
				GPIO_SetBits(DRV8323_POWER_GPIO_PORT,DRV8323_POWER_PIN);
            }
            delay_us(1);
        }
    }
}
void SPI_Drv8323_Init(void)
{
	
//	GPIO_SetBits(DRV8323_POWER_GPIO_PORT,DRV8323_POWER_PIN); //Pmos power on >5ms
//	delay_ms(10);
	motor_power_on();
	GPIO_SetBits(DRV8323_ENABLE_GPIO_PORT,DRV8323_ENABLE_PIN);//reset 8-40us
	delay_ms(2);
	GPIO_ResetBits(DRV8323_ENABLE_GPIO_PORT,DRV8323_ENABLE_PIN);	
	delay_us(25);
	GPIO_SetBits(DRV8323_ENABLE_GPIO_PORT,DRV8323_ENABLE_PIN); 
	delay_ms(2);
	
	TxData=DRV8323_W|DRV8323_ADDRESS6|VREF_DIV; // Before calibration set VREF_DIV VREF/2
	RxData=Spi_Send_HalfWord(TxData);		  
	delay_ms(1);
	
	GPIO_SetBits(DRV8323_CAL_GPIO_PORT,DRV8323_CAL_PIN);   // drv8323 CAL=1 amplifier  calibration 100us
	delay_us(120);
	GPIO_ResetBits(DRV8323_CAL_GPIO_PORT,DRV8323_CAL_PIN);   // drv8323 CAL=0 ,sample current
	delay_us(120);

	TxData=DRV8323_W|DRV8323_ADDRESS6|VREF_DIV|CSA_GAIN|SEN_LVL;//10-V/V current sense amplifier gain VREF_DIV VREF/2
	RxData=Spi_Send_HalfWord(TxData);							// Before calibration set VREF_DIV VREF/2
	delay_ms(1);
	

	
	TxData=DRV8323_W|DRV8323_ADDRESS2|PWM_MODE|OTW_REP;
	RxData=Spi_Send_HalfWord(TxData);
	delay_us(1);
	

	
	TxData=DRV8323_W|DRV8323_ADDRESS3|UNLOCK|IDRIVEP_HS|IDRIVEN_HS;//00b = 500-nspeak gate-current drive time
	RxData=Spi_Send_HalfWord(TxData);
	delay_us(1);


	TxData=DRV8323_W|DRV8323_ADDRESS4|CBC|TDRIVE|IDRIVEP_LS|IDRIVEN_LS;//00b = 500-nspeak gate-current drive time
	RxData=Spi_Send_HalfWord(TxData);
	delay_us(1);
	
//	TxData=DRV8323_W|DRV8323_ADDRESS3|LOCK|IDRIVEP_HS|IDRIVEN_HS;//00b = 500-nspeak gate-current drive time
//	RxData=Spi_Send_HalfWord(TxData);
//	delay_us(1);

	
	TxData=DRV8323_W|DRV8323_ADDRESS5|TRETRY|DEAD_TIME|OCP_MODE|OCP_DEG|VDS_LVL;//00b = 50-ns dead time
	RxData=Spi_Send_HalfWord(TxData);
	delay_us(1);
	

	TxData=DRV8323_R|DRV8323_ADDRESS1;
	RxData=Spi_Send_HalfWord(TxData);
	delay_us(1);
	
	TxData=DRV8323_R|DRV8323_ADDRESS0;
	RxData=Spi_Send_HalfWord(TxData);
	delay_us(1);
	

}


void MotorDrv_SPI_Init(void)
{

	SPI_GPIO_Init();
	SPI_Mode_Init();
	SPI_Drv8323_Init();
}



void Motor_Brake(void)
{

	hTimePhA=PWM_PERIOD-1;
	hTimePhB=PWM_PERIOD-1;
	hTimePhC=PWM_PERIOD-1;
	
    MOTOR_TIM->CCR1=hTimePhA;
    MOTOR_TIM->CCR2=hTimePhB;
    MOTOR_TIM->CCR3=hTimePhC;

    TIM_CtrlPWMOutputs(MOTOR_TIM, DISABLE);
    TIM_CCxCmd(MOTOR_TIM, TIM_Channel_1, TIM_CCx_Disable);
    TIM_CCxCmd(MOTOR_TIM, TIM_Channel_2, TIM_CCx_Disable);
    TIM_CCxCmd(MOTOR_TIM, TIM_Channel_3, TIM_CCx_Disable);
	TIM_CCxCmd(MOTOR_TIM, TIM_Channel_4, TIM_CCx_Disable);
	
    TIM_CCxNCmd(MOTOR_TIM, TIM_Channel_1, TIM_CCxN_Disable);
    TIM_CCxNCmd(MOTOR_TIM, TIM_Channel_2, TIM_CCxN_Disable);
    TIM_CCxNCmd(MOTOR_TIM, TIM_Channel_3, TIM_CCxN_Disable);
    TIM_Cmd(MOTOR_TIM, DISABLE);

}

void Motor_Start(void)
{
	
	hTimePhA=PWM_PERIOD/2-1;
	hTimePhB=PWM_PERIOD/2-1;
	hTimePhC=PWM_PERIOD/2-1;
    
	MOTOR_TIM->CCR1=hTimePhA;
	MOTOR_TIM->CCR2=hTimePhB;
	MOTOR_TIM->CCR3=hTimePhC;
	
    TIM_CCxCmd(MOTOR_TIM, TIM_Channel_1, TIM_CCx_Enable);
    TIM_CCxCmd(MOTOR_TIM, TIM_Channel_2, TIM_CCx_Enable);
    TIM_CCxCmd(MOTOR_TIM, TIM_Channel_3, TIM_CCx_Enable);
	TIM_CCxCmd(MOTOR_TIM, TIM_Channel_4,TIM_CCx_Enable);
	
    TIM_CCxNCmd(MOTOR_TIM, TIM_Channel_1, TIM_CCxN_Enable);
    TIM_CCxNCmd(MOTOR_TIM, TIM_Channel_2, TIM_CCxN_Enable);
    TIM_CCxNCmd(MOTOR_TIM, TIM_Channel_3, TIM_CCxN_Enable);
    TIM_Cmd(MOTOR_TIM, ENABLE);
    TIM_CtrlPWMOutputs(MOTOR_TIM, ENABLE);
  
}


void Motor_Stop(void)
{

	hTimePhA=0;
	hTimePhB=0;
	hTimePhC=0;
	
	MOTOR_TIM->CCR1=hTimePhA;
	MOTOR_TIM->CCR2=hTimePhB;
	MOTOR_TIM->CCR3=hTimePhC;
	
    TIM_CtrlPWMOutputs(MOTOR_TIM, DISABLE);
	
    TIM_CCxCmd(MOTOR_TIM, TIM_Channel_1, TIM_CCx_Disable);
    TIM_CCxCmd(MOTOR_TIM, TIM_Channel_2, TIM_CCx_Disable);
    TIM_CCxCmd(MOTOR_TIM, TIM_Channel_3, TIM_CCx_Disable);
	TIM_CCxCmd(MOTOR_TIM, TIM_Channel_4, TIM_CCx_Disable);
	
    TIM_CCxNCmd(MOTOR_TIM, TIM_Channel_1, TIM_CCxN_Disable);
    TIM_CCxNCmd(MOTOR_TIM, TIM_Channel_2, TIM_CCxN_Disable);
    TIM_CCxNCmd(MOTOR_TIM, TIM_Channel_3, TIM_CCxN_Disable);
	TIM_Cmd(MOTOR_TIM, DISABLE);
}



void Motor_Enable(void)
{
    GPIO_SetBits(DRV8323_ENABLE_GPIO_PORT,DRV8323_ENABLE_PIN);
}

void Motor_Disable(void)
{
	GPIO_ResetBits(DRV8323_ENABLE_GPIO_PORT,DRV8323_ENABLE_PIN);
}






























